package org.yty.travelagencymongodb.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.yty.travelagencymongodb.model.Entity.Result;
import org.yty.travelagencymongodb.model.Entity.Route;
import org.yty.travelagencymongodb.service.RouteService;

@RestController
@RequestMapping("route")
@RequiredArgsConstructor
public class RouteController {
    private final RouteService routeService;

    @GetMapping("/{id}")
    public Result<Route> getById(@PathVariable Long id) {
        Route route = routeService.getById(id);
        if (route == null){
            return Result.error("暂无数据");
        }
        return Result.ok(route);
    }

    @PostMapping
    public Result<Void> save(@RequestBody Route route) {
        boolean isSuccess = routeService.save(route);
        if (!isSuccess){
            return Result.error("保存失败");
        }
        return Result.ok();
    }

    @PutMapping
    public Result<Void> update(@RequestBody Route route) {
        boolean isSuccess = routeService.update(route);
        if (!isSuccess){
            return Result.error("更新失败");
        }
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        routeService.removeById(id);
        return Result.ok();
    }
}
